 $(document).ready(function() {
             //		权限判断
		if($.cookie('ID') == null){
			window.location.href="http://www.duoweish.com/crm/login.html";
		}
//		2，3号权限
		if($.cookie('Right') == 1){
			window.location.href="http://www.duoweish.com/crm/index.html";
		}
			$('.dw_name').text($.cookie('Name'));
		 $('.dw_part').text($.cookie('Department'));
 	
			  var page_num = 0;		
             $(".select2").select2();
             $('#reservation').daterangepicker({autoUpdateInput:false});
             
             init(1);
             $('.search_btn').click(function(){
             	 
			  Ajax(1)
				
            })
            function Ajax(curr){
			      var sea_text =$.trim($('.search_text').val());//搜索框内容
				 
				
            	$.ajax({
				headers:{
				    	Accept:"application/json;charset=utf-8"
				    },
					type:"get",
					url:"http://www.duoweish.com/apis/ProductInfo/",
					data:{
                            UserId:$.cookie('ID'),
							Content:sea_text,
							page:curr||1,
							rows:8
					},
					success:function(data){
						console.log(curr)
						if(data.length!=0){
							page_num = data[1];
						    tableData(data);
					     	divePage(page_num,curr);
						}else{
							layer.msg('没找到该商家')
						}
						
						}
				})
            	
            }
            function tableData(ret){
               
                    $('#table_body').empty();
                    for(var i = 0;i < ret.length;i++){
                    	for (var j = 0;j<ret[i].length;j++) {
                    		
                    	
                        var tr = `<tr>
                                <td style="line-height:20px;"><input type=text value="${ret[i][j][0]}"/></td>
                                <td style="line-height:20px;"><input type=text value="${ret[i][j][1]}"/></td>
                                <td style="line-height:20px;"><input type=text value="${ret[i][j][2]}"/></td>
                                <td style="line-height:20px;"><input type=text value="${ret[i][j][3]}"/></td>
                                
                                <td style="line-height:20px;"><input type=text value="${ret[i][j][4]}"/></td>
                                <td style="line-height:20px;"><input type=text value="${ret[i][j][5]}"/></td>
                                <td style="line-height:20px;"><input type=text value="${ret[i][j][6]}"/></td>
                                <td index="${ret[i][j][7]}">
                                  <button class="edit_btn check1" index="1"><i style="font-size: 20px;" class="fa fa-pencil"></i></button>
                                	<span style="display:inline-block;width:20px;"></span>
                                	<button class="edit_btn check2" index="0"><i style="font-size: 20px;" class="fa fa-trash-o"></i></button>
                                </td>
                               
                            </tr>`;
                        $('#table_body').append(tr);
                    }
                }
                   
                   
                   
                   
                 $('.check1').click(function(){
                 	     var product_data=[];
                 	     var index = $(this).parent().attr("index");
						for (var j = 0;j<$(this).parent().parent().find("td").length-1;j++ ) {
						  product_data.push($(this).parent().parent().find("td").eq(j).find("input").val());
					   }
						$.ajax({
							type:"get",
							url:"http://www.duoweish.com/apis/ProductEdit/",
							data:{
								Product:index,
								Info:product_data.join('*@*')
							},
							success:function(data){
								if(data == "failure"){
									layer.msg('编辑失败，请重试');
								}else{
								layer.msg('编辑成功');	
								}
							}
						})
                 })
                 $('.check2').click(function(){
                 	    var index = $(this).parent().attr("index");
						$.ajax({
							type:"get",
							url:"http://www.duoweish.com/apis/ProductDel/",
							data:{
								Product:index,
							},
							success:function(){
  								layer.msg('删除成功');
  								$('#table_body').empty();
  								Ajax(1);
							}
						})
                 })
                 
      			$('.add_new').click(function(){
      				layer.open({
					type: 2
					,title: "新增产品"
					,closeBtn: 1
					,area:['400px','500px']
					,shade: 0.8
					,id: 'LAY_layuipro' 
					,shadeClose:true		       
					,btnAlign: 'c'
					,moveType: 1 
					,content:'http://www.duoweish.com/crm/new_product.html'
					,success: function(){
									    	
						         }	    			  
							})
      			})
                    
            }
          
           function divePage(page_num,curr){
            layui.use(['laypage', 'layer'], function(){
					 var laypage = layui.laypage
			  		,layer = layui.layer;
			  	
                     laypage.render({
						    elem: 'demo0'
						    ,count: page_num
						    ,limit:8
						    ,curr:curr
						    ,jump: function(obj, first){
						    	   if(!first){
						                Ajax(obj.curr);
						               }
				        
						      		}
						   		});
					 })		
           }
          
           function init(curr){ 
           	     Ajax(1)
	            divePage(page_num);
           }
          
     
           
})